package com.iaz.tech.tools.uml.file.impl;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.iaz.tech.tools.common.utils.UtilTools;
import com.iaz.tech.tools.uml.IUMLNeuronAble; 
import com.iaz.tech.tools.uml.structural.classstruct.impl.NeuronEnumStruct;
import com.iaz.tech.tools.uml.structural.classstruct.impl.NeuronParameterStruct;

import lombok.Data;
import lombok.EqualsAndHashCode;

@EqualsAndHashCode(callSuper=false)
@Data
public class NeuronEnumLoader extends NeuronParameterStructLoader  {
    /**
     * 日志
     */
	private final static Logger logger = (Logger) LoggerFactory.getLogger(NeuronEnumLoader.class);

    @Override
    public NeuronParameterStruct toParameterStruct(IUMLNeuronAble clazz) {
        if (clazz == null) {
            throw new IllegalArgumentException("umlObj 不能为空");
        }
        if (!(clazz instanceof NeuronEnumStruct)){
            throw new IllegalArgumentException("umlObj 必须是 NeuronEnumStruct 类型");
        } 
        NeuronEnumStruct clazz_struct = (NeuronEnumStruct) clazz;
        return clazz_struct;
    }
    
    @Override
    public void parseJSON(IUMLNeuronAble umlObj, JSONObject json) {
        try {
            if (umlObj == null) {
                throw new Exception("umlObj 不能为空");
            }
            if (json == null) {
                throw new Exception("json 不能为空");
            }
             
            NeuronEnumStruct enum_struct = (NeuronEnumStruct)this.toParameterStruct(umlObj);
            if (json.containsKey("name")) {
                enum_struct.setName(json.getString("name"));
            } else {
                throw new Exception("name 必须输入");
            }

            if (json.containsKey("values")) {
                JSONArray values = json.getJSONArray("values");
                for (int i = 0; i < values.size(); i++) {
                    JSONObject value = values.getJSONObject(i);
                    String value_name = value.toString();
                    if (!UtilTools.IsNull(value_name)) {
                        enum_struct.getValues().add(value_name);
                    }
                }
            }
                  
        } catch(Exception e) {
            logger.error("出现异常：" + e.getMessage());
        } 
    }

    @Override
    public JSONObject toJSON(IUMLNeuronAble objStruct) {
        JSONObject result = new JSONObject();
        try {
            if (objStruct == null) {
                throw new Exception("objStruct 不能为空");
            }
            if (!(objStruct instanceof NeuronEnumStruct)) {
                throw new Exception("objStruct 不是 NeuronEnum 类型"); 
            } 
            NeuronEnumStruct enmu = (NeuronEnumStruct)this.toParameterStruct(objStruct);
            result = super.toJSON(objStruct);
            result.put("values", enmu.getValues()); 
        } catch(Exception e) {
            logger.error("出现异常：" + e.getMessage());
            result = new JSONObject();
        }
        return result;
    }
}
